/**
 * Copyright [2019] [LiBo/Alex of copyright liboware@gmail.com ]
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.dess.base.tools.delayed;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.DelayQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;

/**
 * @project-name:dess
 * @package-name:com.hyts.dess.base.tools.delayed
 * @author:LiBo/Alex
 * @create-date:2020-08-11 19:22
 * @copyright:libo-alex4java
 * @email:liboware@gmail.com
 * @description:
 */
@AllArgsConstructor
@Slf4j
public abstract class DataMonitor implements Runnable{

    /**
     * 延时队列 ,消费者从其中获取消息进行消费
     */
    private DelayQueue<DelayedEvent> queue;

    @Override
    public void run() {
        while (true) {
            try {
                DelayedEvent take = queue.take();
                execute(take);
                LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(5));
            } catch (InterruptedException e) {
                log.error("操作执行失败功能实现!",e);
            }
        }
    }

    public abstract void execute(DelayedEvent take);
}
